package com.feihong.txgw.ui.activity.product.webviewinterface;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.webkit.JavascriptInterface;
import com.blankj.utilcode.util.LogUtils;
import com.feihong.txgw.ui.activity.product.ProductInfoActivity;
import com.just.agentweb.AgentWeb;

/**
 * Created by Sabirjan on 2017/7/6.
 * TXGWApp.
 * 飞宏电子
 */

public class ProductInfoWebViewInterface {

    private AgentWeb agent;
    private Context context;
    private String productId = "";
    private int count;
    private ProductInfoActivity.ActionCallBack callBack;

    public ProductInfoWebViewInterface(AgentWeb agent, Context context, String productId, int count, ProductInfoActivity.ActionCallBack callBack) {
        this.agent = agent;
        this.context = context;
        this.callBack = callBack;
        this.count = count;
    }

    private Handler deliver = new Handler(Looper.getMainLooper());

    //提交订单
    @JavascriptInterface
    public void submitOrder(final String msg) {

        deliver.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.buy(msg);
                }

            }
        });

        //对外接口
    }

    //加入购物车
    @JavascriptInterface
    public void addCart(final String msg) {

        deliver.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.addcart(msg);
                }
            }
        });

        //对外接口
    }

    //返回购物车数量
    @JavascriptInterface
    public int cart() {
        LogUtils.i("--------------返回购物车数量--------------"+ count);
        return count;
    }

    //点击购物车
    @JavascriptInterface
    public void gotoCart() {
        LogUtils.i("--------------去购物车页面----------");
        deliver.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.gotoCart();
                }
            }
        });
    }
}
